package com.tsystems.train.bean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import com.tsystems.train.entity.Train;

@FacesConverter(value = "trainConverter", forClass = Train.class)
public class TrainConverter implements Converter {

	public static List<Train> trains = new ArrayList<Train>();

	public Object getAsObject(FacesContext facesContext, UIComponent component,
			String submittedValue) {
		if (submittedValue.trim().equals("")) {
			return null;
		} else {
			for (Train t : trains) {
				if (t.getNumber().equals(submittedValue)) {
					return t;
				}
			}

		}

		return null;
	}

	public String getAsString(FacesContext facesContext, UIComponent component,
			Object value) {
		if (value == null || value.equals("")) {
			return "";
		} else {
			return ((Train) value).getNumber();
		}
	}

}
